package com.lvy.hczn.front.business.task;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.lvy.hczn.front.business.service.*;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.system.domain.business.SaleOrder;
import com.lvy.hczn.front.system.domain.business.TransferOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 前置仓定时任务
 *
 * @author yw
 * @date 2023-06-12 15:21:34
 **/
@Component("businessTask")
@Slf4j
public class BusinessTask {

    @Autowired
    private SaleOrderService saleOrderService;

    @Autowired
    private SortTaskService sortTaskService;

    @Autowired
    private AccessManagerService accessManagerService;

    @Autowired
    private TransferOrderService transferOrderService;

    @Autowired
    private WcsService wcsService;

    @Autowired
    private ShelvesService shelvesService;

    @Autowired
    private ExceptionService exceptionService;

    /**
     * 定时生成销售分拣任务
     *
     * @param
     * @return void
     * @author yw
     * @date 2023-06-13 14:18:21
     */
    public void sortTask(String minute) {
        SaleOrder saleOrder = new SaleOrder();
        LocalDateTime time = LocalDateTime.now();
        if (StrUtil.isEmpty(minute) || !NumberUtil.isInteger(minute)) {
            time = time.plusMinutes(Long.parseLong("70"));
        } else {
            time = time.plusMinutes(Long.parseLong(minute));
        }
        saleOrder.setDeliveryTime(time);
        saleOrder.setType(com.lvy.hczn.front.common.constant.Constants.SALE_ORDER_TYPE_ONE);
        saleOrder.setStatus(com.lvy.hczn.front.common.constant.Constants.SALE_ORDER_STATUS_ZERO);
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        sortTaskService.generateSortTask(list, false);
        log.error("销售分拣定时");
        this.sortTransferTask();
    }

    /**
     * 定时生成调拨分拣任务
     *
     * @param
     * @return void
     * @author yw
     * @date 2023-06-13 14:18:21
     */
    @Deprecated
    public void sortTransferTask() {
        TransferOrder transferOrder = new TransferOrder();
        transferOrder.setStatus(Constants.TRANSFER_ORDER_STATUS_ZERO);
        List<TransferOrder> list = transferOrderService.selectIncludeDetailByStatus(transferOrder);
        sortTaskService.generateSortTaskByTransfer(list);
        log.error("调拨分拣定时");
    }

    /**
     * 门禁失效
     *
     * @param hours 间隔多长时间失效（单位：小时）
     * @param types 门禁类型：1：分拣、2：配送单(发货配送)；3、运输单；4：货箱租赁；5：配送单（退货配送，拒收配送）；6、供应商自配到仓库，7：销售配送(配送员入仓通知)
     *              eg:'5' '1,2,3,4,5'
     * @return void
     * @author yw
     * @date 2023-07-22 17:17:10
     */
    public void accessManager(String hours, String types) {
        try {
            accessManagerService.checkInvalidAccessManagerCode(hours, types);
        } catch (Exception e) {
            log.error("门禁定时异常：{}", e);
        }
    }

    /**
     * wcs堆箱占比定时任务
     *
     * @param
     * @return void
     * @author yw
     * @date 2023-10-21 20:21:48
     */
    public void stackBoxesPercent(String percent) {
        try {
            wcsService.stackBoxesPercent(percent);
        } catch (Exception e) {
            log.error("堆箱占比定时任务异常", e);
        }
    }

    /**
     * 物理货位定时任务初始化，理论一小时初始化一次，因为要配送，需整点分拣完
     * 最好的方式是，配送员取货触发，目前平台未开发对接，暂时定时
     *
     * @param volume
     * @return
     * @author yw
     * @date 2023-12-20 15:12:57
     */
    public void initShelvesTask(String volume) {
        try {
            shelvesService.initShelvesTask(volume);
        } catch (Exception e) {
            log.error("物理货位定时任务执行异常:{}", e);
        }
    }

    /**
     * 数据定时清理
     *
     * @param days
     * @return
     * @author yw
     * @date 2023-12-20 15:12:57
     */
    public void dataClean(String days) {
        try {
            exceptionService.dataClean(days);
        } catch (Exception e) {
            log.error("数据定时清理异常:", e);
        }
    }
}
